package at.ac.tuwien.lsds.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class SchedulerOutputFile implements OutputFile{
	private List<SchedulerOutputRow> rowList = new ArrayList<SchedulerOutputRow>();
	private static SchedulerOutputFile instance = null;
	private static final Logger log = Logger.getLogger(SchedulerOutputFile.class);
	
	private DecimalFormat df = new DecimalFormat(Settings.getDecimalFormat());
	
//	private String scenario = "";
	private String scheduler = "";
	
	private SchedulerOutputFile() {
	}
	
	public static SchedulerOutputFile getInstance(){	
		if(null == instance){
			synchronized (SchedulerOutputFile.class) {
				instance = new SchedulerOutputFile();
			}
		}
		
		return instance;
	}
	
	@Override
	public String getHeadline() {
		return "Timestamp (ms)"+Settings.getSeparator()+"TotalRAM (MB)"+Settings.getSeparator()+"TotalCPU (MHz)"+Settings.getSeparator()+"TotalSize (MB)"+Settings.getSeparator()+"RunningPMs"+Settings.getSeparator()+"RunningVMs"+Settings.getSeparator()+"TotalPowerConsumption (W)"+Settings.getSeparator()+"InSourced"+Settings.getSeparator()+"OutSourced";
	}
	
	@Override
	public List<String> getContentRows() {
		List<String> stringRowList = new ArrayList<String>();
		
		for (SchedulerOutputRow row : rowList) {
			stringRowList.add(this.rowToString(row));
		}
		return stringRowList;
	}
	
	
	@Override
	public String getFilePath() {
		return Settings.getSchedulerOutputDirectory() + "/" + scheduler + "_" + Settings.getScenario() + ".csv";
	}
	public void clear(){
		rowList = new ArrayList<SchedulerOutputRow>();
		df = new DecimalFormat(Settings.getDecimalFormat());
//		scenario = "";
//		scheduler = "";
	}

	public void addRow(SchedulerOutputRow row){
		rowList.add(row);
		log.debug(this.rowToString(row));
	}

//	public String getScenario() {
//		return scenario;
//	}
//
//	public void setScenario(String scenario) {
//		this.scenario = scenario;
//	}
//
	public String getScheduler() {
		return scheduler;
	}

	public void setScheduler(String scheduler) {
		this.scheduler = scheduler;
	}
	
	private String rowToString(SchedulerOutputRow row) {
		String out = "";
		out += row.getTimestamp().toString();
		out += Settings.getSeparator() + row.getTotalRam().toString();
		out += Settings.getSeparator() + row.getTotalCpu().toString();
		out += Settings.getSeparator() + row.getTotalSize().toString();
		out += Settings.getSeparator() + row.getRunningPMs().toString();
		out += Settings.getSeparator() + row.getRunningVMs().toString();
		out += Settings.getSeparator() + df.format(row.getTotalPowerConsumption());
		out += Settings.getSeparator() + row.getInSourced().toString();
		out += Settings.getSeparator() + row.getOutSourced().toString();
		return out;
	}
}
